package com.ruanda.spongebobrunner;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.ruanda.spongebobrunner.game.GameView;
import com.ruanda.spongebobrunner.ui.LoadingView;

public class MainActivity extends Activity {
    private GameView gameView;
    private LoadingView loadingView;
    private FrameLayout container;
    private Handler handler = new Handler();
    private int progressStatus = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // 刘海屏适配
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            getWindow()
                    .getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        // 创建容器布局
        container = new FrameLayout(this);
        setContentView(container);

        // 创建并添加加载视图
        loadingView = new LoadingView(this);
        container.addView(loadingView);

        // 开始加载过程
        startLoading();
    }

    private void startLoading() {
        // 模拟加载进度
        new Thread(() -> {
            while (progressStatus < 100) {
                progressStatus += 1;

                try {
                    Thread.sleep(30); // 总共约3秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 更新加载视图
                handler.post(() -> {
                    loadingView.setProgress(progressStatus);

                    // 更新加载文本
                    if (progressStatus < 30) {
                        loadingView.setLoadingText(0); // "正在加载资源..."
                    } else if (progressStatus < 60) {
                        loadingView.setLoadingText(1); // "正在初始化游戏..."
                    } else {
                        loadingView.setLoadingText(2); // "即将开始..."
                    }
                });
            }

            // 加载完成后创建GameView
            handler.post(() -> {
                // 移除加载视图
                container.removeView(loadingView);

                // 创建GameView并添加到布局中
                gameView = new GameView(MainActivity.this);
                container.addView(gameView);
            });
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (gameView != null) {
            gameView.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (gameView != null) {
            gameView.pause();
        }
    }
}
